// 属性列表
controllers.controller('propertyListCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Property', 'AlertService',
    function ($scope, $http, $rootScope, $modal, $window, Property, AlertService) {
        $scope.resetCurrentPageName("属性维护");
        $scope.utils = Food.utils;
        $scope.pageSize = 20;
        $scope.currentPage = 1;
        $scope.propertyList = Property.propertyList;
        $scope.title = "";
        $scope.propertyType = 1;
        $scope.currentProperty = Property.currentProperty;

        $scope.queryList = function () {
            Property.queryList($scope.title, $scope.propertyType, $scope.currentPage, $scope.pageSize, function () {
                $scope.totalItems = Property.totalSceneCount;
            });
        };
        $scope.deleteProperty = function (id) {
            Property.delete(id, function () {
            });
        };
        $scope.propertyDetail = function (property) {
            Property.currentProperty = property;
            $window.location.href = "#property/propertyDetail";
        };
        $scope.queryList();

        var operateTemplate = "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.deleteProperty(row.entity.id)'>删除</button>" +
            "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.propertyDetail(row.entity)'>属性详情</button>";

        var propertyTypeTemplate = "<span ng-if='row.entity.propertyType==1'>sku</span>" +
            "<span ng-if='row.entity.propertyType==2'>其他</span>" +
            "<span ng-if='row.entity.propertyType==3'>品牌</span>";

        $scope.gridOptions = {
            data: 'propertyList',
            enableColumnMenus: false,
            enableColumnResizing: true,
            rowHeight: 40,
            columnDefs: [

                {field: 'title', displayName: '属性名称'},
                {
                    field: 'propertyType',
                    displayName: '属性类型',
                    cellTemplate: propertyTypeTemplate
                },
                {field: 'editType', displayName: '属性编辑类型'},
                {field: 'userId', displayName: '创建者Id'},
                {
                    field: 'status',
                    displayName: '状态',
                    cellTemplate: '<div>{{row.entity.state == 0 ? "停用" :"在用"}}</div>'
                },
                {
                    field: 'id',
                    displayName: '操作',
                    width: "30%",
                    cellTemplate: operateTemplate
                }
            ]
        };
    }]);

// 属性列表,选择情况下
controllers.controller('propertyListSelectCtrl', ['$scope', '$http', '$rootScope', '$modal', 'Property', 'AlertService', function ($scope, $http, $rootScope, $modal, Property, AlertService) {
    $scope.utils = Food.utils;
    $scope.pageSize = 20;
    $scope.propertyList = Property.propertyList;
    $scope.title = "";
    $scope.propertyType;
    $rootScope.selectedItemList = [];

    $scope.innerOk = function () {
        //获取已选择的列表
        for (var entity in $scope.propertyList) {
            if ($scope.propertyList[entity].selected) {
                $rootScope.selectedItemList.push($scope.propertyList[entity]);
            }
        }
        $rootScope.ok();
    };

    $scope.queryList = function () {

        Property.queryList($scope.title, $scope.propertyType, $scope.currentPage, $scope.pageSize, function () {
            $scope.totalItems = Property.totalSceneCount;
        });
    };
    $scope.queryList();
    $scope.selectedChange = function (entity) {
        if (!entity.selected) {
            entity.selected = true;
        } else {
            entity.selected = !entity.selected;
        }
    }

    $scope.gridOptions = {
        data: 'propertyList',
        enableColumnMenus: false,
        enableColumnResizing: true,
        rowHeight: 40,
        columnDefs: [
            {
                field: 'id',
                displayName: '属性名称',
                cellTemplate: '<input type="checkbox" ng-model="checked" ng-change="grid.appScope.selectedChange(row.entity)" />'
            },
            {field: 'title', displayName: '属性名称'},
            {field: 'propertyType', displayName: '属性类型'},
            {field: 'editType', displayName: '属性编辑类型'},
            {field: 'userId', displayName: '创建者Id'},
            {
                field: 'status',
                displayName: '状态',
                cellTemplate: '<div>{{row.entity.state == 0 ? "停用" :"在用"}}</div>'
            }
        ]
    };
}]);

// 新增属性
controllers.controller('addPropertyCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Property', 'AlertService', function ($scope, $http, $rootScope, $modal, $window, Property, AlertService) {
    $scope.resetCurrentPageName("属性维护");
    $scope.title = "";
    $scope.editType = "";
    $scope.propertyType = "";

    $scope.addProperty = function () {
        Property.insert($scope.title, $scope.editType, $scope.propertyType, $scope.couldAppendImage, function () {
            AlertService.addAlert({type: 'success', msg: '操作成功! '}, 1000);
            //更新页面哦
            $window.location.href = "#property/propertyList";
        });
    };

}]);

// 属性详情
controllers.controller('propertyDetailCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'Property', 'AlertService', function ($scope, $http, $rootScope, $modal, $window, Property, AlertService) {
    $scope.resetCurrentPageName("属性维护");
    $scope.currentProperty = Property.currentProperty;
    $scope.propertyValueList = Property.propertyValueList;
    $scope.totalItems = Property.propertyValueCount;
    $scope.currentPage = 1;
    $scope.pageSize = 10;
    $scope.propertyValue = "";

    $scope.queryList = function () {

        Property.getPropertyValues($scope.currentProperty.id, $scope.currentPage, $scope.pageSize, function () {

        });
    };
    $scope.queryList();

    var operateTemplate = "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.deletePropertyValue(row.entity.id)'>删除</button>";
    $scope.gridOptions = {
        data: 'propertyValueList',
        enableColumnMenus: false,
        enableColumnResizing: true,
        rowHeight: 40,
        columnDefs: [
            {field: 'id', displayName: 'id'},
            {field: 'value', displayName: '属性值'},
            {
                field: 'id',
                displayName: '操作',
                width: "30%",
                cellTemplate: operateTemplate
            }
        ]
    };
    $scope.deletePropertyValue = function (id) {
        Property.deletePropertyValue(id, function () {
            AlertService.addAlert({type: 'success', msg: '操作成功! '}, 1000);
        });
    };

    $scope.newPropertyValue = function () {

        if (!$scope.propertyValue || /^\s*$/.test($scope.propertyValue)) {
            AlertService.addAlert({type: 'danger', msg: '属性值不能为空! '}, 1000);
        } else {
            Property.insertPropertyValue($scope.currentProperty.id, $scope.currentProperty.title, $scope.currentProperty.couldAppendImage, $scope.propertyValue, function () {
                AlertService.addAlert({type: 'success', msg: '操作成功! '}, 1000);
            });
        }
    };

}]);

// 新增属性值
controllers.controller('addPropertyValueCtrl', ['$scope', '$http', '$rootScope', '$modal', 'Property', 'AlertService', function ($scope, $http, $rootScope, $modal, Property, AlertService) {
    $scope.resetCurrentPageName("属性维护");
    $scope.propertyMetaId = "";
    $scope.value = "";

    $scope.addPropertyValue = function () {
        Property.insertPropertyValue($scope.propertyMetaId, $scope.value, function () {
            AlertService.addAlert({type: 'success', msg: '操作成功! '}, 1000);
        });
    };

}]);

